//
//  XMWebView.m
//  XMWebTool
//
//  Created by ext.zhangmingwei8 on 2023/12/7.
//

#import "XMWebView.h"
#import <WebKit/WebKit.h>
#import "UIColor+XMTool.h"
#import "XMSizeMacro.h"
#import "UIView+XMFrame.h"
#import "XMWebViewManager.h"
#import "UIView+XMTool.h"
#import "XMToast.h"

@interface XMWebView()

@property (nonatomic, assign) CGFloat   xScale;
@property (nonatomic, assign) CGFloat   yScale;
/// 蒙层
@property (nonatomic, strong) UIView    *maskView;

@property (nonatomic, assign) CGFloat   oldX;
@property (nonatomic, assign) CGFloat   oldY;

@end

@implementation XMWebView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    [self addSubview:self.bottomV];
    [self addSubview:self.maskView];
    self.clipsToBounds = YES;
    self.layer.masksToBounds = YES;
    self.layer.cornerRadius = 20;
    self.layer.borderColor = [UIColor colorFromHexString:@"#61C355" alpha:0.3].CGColor;
    self.layer.borderWidth = 5.0;
    
    __weak typeof(self) weakSelf = self;
    [self.maskView setTapActionWithBlock:^{
        [weakSelf showBigAction];
    }];
    [self.bottomV.closeBtn setTapActionWithBlock:^{
        [weakSelf showSmallAction];
    }];
    [self.bottomV.reloadBtn setTapActionWithBlock:^{
        [weakSelf.webView reload];
    }];
    [self.bottomV.backBtn setTapActionWithBlock:^{
        [weakSelf.webView goBack];
    }];
    [self.bottomV.favoriteBtn setTapActionWithBlock:^{
        [UIPasteboard generalPasteboard].string = weakSelf.webView.URL.absoluteString;
        [XMToast showTextToCenter:@"复制链接成功"];
    }];
    
    return self;
}

- (void)addUrlString:(NSString *)urlString {
    self.urlString = urlString;
    if (self.webView == nil) {
        self.webView = [[XMWebViewManager defalutManager] getWebView:self.urlString];
        [self addSubview:self.webView];
    }
    self.webView.frame = CGRectMake(0, 0, self.width, self.height - (50+kSafeAreaBottom_XM));
    [self bringSubviewToFront:self.maskView];
    
}

- (void)setScaleWithX:(CGFloat)xScale yScale:(CGFloat)yScale {
    self.xScale = xScale;
    self.yScale = yScale;
}

// 展示大的状态
- (void)showBigAction {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"kNotifi_showBig" object:nil];
    self.oldX = self.origin.x;
    self.oldY = self.origin.y;
    UIScrollView *superScrollV = (UIScrollView *)self.superview;
    superScrollV.scrollEnabled = NO;

    [UIView animateWithDuration:0.2 animations:^{
        [self.superview bringSubviewToFront:self];
        self.bottomV.hidden = NO;
        self.maskView.hidden = YES;
        self.layer.borderWidth = 0.0;
        self.layer.cornerRadius = 5;
        self.webView.frame = CGRectMake(0, 0, self.webView.width, kScreenHeight_XM - kStatusBarHeight_XM - (self.bottomV.height));
        self.transform = CGAffineTransformMakeScale(1.0, 1.0);
        self.frame = CGRectMake(0, superScrollV.contentOffset.y, self.width, self.height);
    } completion:^(BOOL finished) {
        
    }];
    
}

// 展示小的状态
- (void)showSmallAction {
    UIScrollView *superScrollV = (UIScrollView *)self.superview;
    superScrollV.scrollEnabled = YES;
    
    [UIView animateWithDuration:0.2 animations:^{
        [self.superview bringSubviewToFront:self];
        self.maskView.hidden = NO;
        self.bottomV.hidden = YES;
        self.layer.borderWidth = 5.0;
        self.layer.cornerRadius = 20;
        self.webView.frame = CGRectMake(0, 0, self.webView.width, kScreenHeight_XM - kStatusBarHeight_XM + 170);
        self.transform = CGAffineTransformMakeScale(self.xScale, self.yScale);
        self.frame = CGRectMake(self.oldX, self.oldY, self.width, self.height);
    } completion:^(BOOL finished) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"kNotifi_showSmall" object:nil];
    }];
}

- (XMWebBottomView *)bottomV {
    if (!_bottomV) {
        _bottomV = [[XMWebBottomView alloc] initWithFrame:CGRectMake(0, self.height - (50+kSafeAreaBottom_XM), kScreenWidth_XM, (50+kSafeAreaBottom_XM))];
    }
    return _bottomV;
}

- (UIView *)maskView {
    if (!_maskView) {
        _maskView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height)];
        _maskView.backgroundColor = [UIColor clearColor];
        // 添加渐变色
        NSArray *colorArr = @[[UIColor colorFromHexString:@"#000000" alpha:0.0], [UIColor colorFromHexString:@"#000000" alpha:0.5], [UIColor colorFromHexString:@"#000000" alpha:0.7], [UIColor colorFromHexString:@"#000000" alpha:0.8]];
        CALayer *gradientLayer = [UIView getGradientLayerWithColorArray:colorArr startPoint:CGPointMake(0.5, 0) endPoint:CGPointMake(0.5, 1) size:CGSizeMake(self.width, self.height)];
        [_maskView.layer addSublayer:gradientLayer];
    }
    return _maskView;
}

@end
